/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018-01-6 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.admin.controller.user;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.admin.controller.GenericController;
import com.itttq.gshop.domain.po.OrderInfoPo;
import com.itttq.gshop.domain.po.UserInfoPo;
import com.itttq.gshop.domain.to.PageParamTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.connector.IOrderInfoConnector;
import com.itttq.gshop.generic.connector.IUserInfoConnector;

@RestController
@RequestMapping("user/info")
public class UserInfoController extends GenericController {
	private static final long serialVersionUID = 997162097056404387L;
	@Autowired
	private IUserInfoConnector userInfoConnector;
	@Autowired
	private IOrderInfoConnector orderInfoConnector;
	
	/**
	 * 根据条件查询UserInfoPo对象，并分页
	 * 
	 * @param userInfo
	 * @param request
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("infos.html")
	public ModelAndView getUserInfos(UserInfoPo userInfo, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/user/infos");
		PageParamTo ppt = null == request ? new PageParamTo() : getPageParam(request);
		userInfo.setStart(ppt.getStart());
		userInfo.setSize(ppt.getSize());
		ResultPageTo<UserInfoPo> rpt = userInfoConnector.getUserInfos(userInfo);
		this.getPageMv("infos", mv, rpt);
		mv.addObject("info", userInfo);
		return mv;
	}

	
	/**
	 * 跳转到新增页面
	 * @return
	 */
	@RequestMapping("add.html")
	public ModelAndView addUserInfo() {
		ModelAndView mv = new ModelAndView("/user/infoAdd");
		mv.addObject("op", "view");
		return mv;
	}
	/**
	 * 跳转到查看页面
	 * @param id
	 * @return
	 */
	@RequestMapping("view/{id}.html")
	public ModelAndView viewUserInfo(@PathVariable("id") Integer id) {
		ModelAndView mv = editUserInfo(id);
		mv.setViewName("/user/infoView");
		// 订单数据
		OrderInfoPo orderInfo = new OrderInfoPo();
		orderInfo.setUserId(id);
		ResultPageTo<OrderInfoPo> rpt = orderInfoConnector.getOrderInfos(orderInfo);
		this.getPageMv("orders", mv, rpt);
		mv.addObject("order", orderInfo);
		return mv;
	}
	/**
	 * 跳转到编辑页面
	 * @param id
	 * @return
	 */
	@RequestMapping("edit/{id}.html")
	public ModelAndView editUserInfo(@PathVariable("id") Integer id) {
		ModelAndView mv = addUserInfo();
		mv.addObject("user", userInfoConnector.getUserInfo(id));
		mv.addObject("op", "edit");
		return mv;
	}
	
	/**
	 * 保存UserInfoPo对象，无ID则新增，有ID则更新
	 * 
	 * @param userInfo
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("save.html")
	public ModelAndView saveUserInfo(UserInfoPo userInfo) {
		userInfoConnector.saveUserInfo(userInfo);
		return getUserInfos(new UserInfoPo(), null);
	}

	/**
	 * 根据ID删除UserInfoPo对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("del/{id}.html")
	@ResponseBody
	public ModelAndView delUserInfo(@PathVariable("id") Integer id) {
		if(id != null) {
			userInfoConnector.delUserInfo(id);
		}
		return getUserInfos(new UserInfoPo(), null);
	}
}